package com.guguskins.back.utils;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;

/**
 * @author duttonliu
 * @date 2021/6/29 17:06
 */
public class DateUtil {

    /**
     * 获取当前时间（String）
     */
    public static String getDate(String format) {
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(format));
    }

    /**
     * 将Long类型的时间戳转换成String 类型的时间格式，时间格式为：yyyy-MM-dd HH:mm:ss
     */
    public static String timeToString(Long time, String format) {
        // "yyyy-MM-dd HH:mm:ss"
        DateTimeFormatter formatString = DateTimeFormatter.ofPattern(format);
        return formatString.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()));
    }


    /**
     * 将Long类型的毫秒级时间戳转换成String 类型的时间格式，时间格式为：yyyy-MM-dd HH:mm:ss
     */
    public static String milliTimeToString(Long time, String format) {
        // "yyyy-MM-dd HH:mm:ss"
        DateTimeFormatter formatString = DateTimeFormatter.ofPattern(format);
        return formatString.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()));
    }

    /**
     * 将Long类型的秒级时间戳转换成String 类型的时间格式，时间格式为：yyyy-MM-dd HH:mm:ss
     */
    public static String secondTimeToString(Long time, String format) {
        // "yyyy-MM-dd HH:mm:ss"
        DateTimeFormatter formatString = DateTimeFormatter.ofPattern(format);
        return formatString.format(LocalDateTime.ofInstant(Instant.ofEpochSecond(time), ZoneId.systemDefault()));
    }


    /**
     * 将String（yyyy-MM-dd HH:mm:ss）类型的毫秒级时间戳转换成LocalDateTime
     */
    public static LocalDateTime strToLocalDateTime(String time) {
        // "yyyy-MM-dd HH:mm:ss"
        return LocalDateTime.parse(time, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    /**
     * 将Long类型的毫秒级时间戳转换成LocalDateTime
     */
    public static LocalDateTime milliTimeToLocalDateTime(Long time) {
        // "yyyy-MM-dd HH:mm:ss"
        return LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault());
    }

    /**
     * 将字符串转日期成Long类型的时间戳，格式为：yyyy-MM-dd HH:mm:ss
     */
    public static Long timeToLong(String time) {
        DateTimeFormatter formatString = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime parse = LocalDateTime.parse(time, formatString);
        return LocalDateTime.from(parse).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    /**
     * localDate 格式化
     */
    public static String localDateFormat(LocalDate localDate, String format) {
        // "yyyy-MM-dd HH:mm:ss"
        return DateUtil.timeToString(localDate2Second(localDate), format);
    }

    /**
     * localDate 格式化
     */
    public static String localDateTimeFormatStr(LocalDateTime localDateTime, String format) {
        // "yyyy-MM-dd HH:mm:ss"
        return DateUtil.timeToString(localDateTime2Second(localDateTime), format);
    }

    /**
     * 取本月第一天
     */
    public static LocalDate firstDayOfThisMonth() {
        LocalDate today = LocalDate.now();
        return today.with(TemporalAdjusters.firstDayOfMonth());
    }

    /**
     * 取本月第一天
     */
    public static LocalDate firstDayOfLastMonth() {
        LocalDate today = LocalDate.now();
        today = today.minusMonths(1);
        return today.with(TemporalAdjusters.firstDayOfMonth());
    }

    /**
     * 取本月第N天
     */
    public static LocalDate dayOfThisMonth(int n) {
        LocalDate today = LocalDate.now();
        return today.withDayOfMonth(n);
    }

    /**
     * 取本月最后一天
     */
    public static LocalDate lastDayOfThisMonth() {
        LocalDate today = LocalDate.now();
        return today.with(TemporalAdjusters.lastDayOfMonth());
    }

    /**
     * 获取本周一
     */
    public static LocalDateTime firstDayOfWeek(Long date) {
        // long转LocalDateTime
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date), ZoneId.systemDefault());
        return localDateTime.with(DayOfWeek.MONDAY);
    }

    /**
     * 获取上周一
     */
    public static LocalDateTime firstDayOfLastWeek(Long date) {
        // long转LocalDateTime
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date), ZoneId.systemDefault());
        LocalDateTime localDateTime1 = localDateTime.plusDays(-7);
        return localDateTime1.with(DayOfWeek.MONDAY);
    }

    /**
     * 取本月第一天的开始时间
     */
    public static LocalDateTime startOfThisMonth() {
        return LocalDateTime.of(firstDayOfThisMonth(), LocalTime.MIN);
    }

    /**
     * 取本月最后一天的结束时间
     */
    public static LocalDateTime endOfThisMonth() {
        return LocalDateTime.of(lastDayOfThisMonth(), LocalTime.MAX);
    }


    /**
     * LocalDate转毫秒级时间戳
     */
    public static Long localDate2Milli(LocalDate localDate) {
        return LocalDateTime.of(localDate, LocalTime.MIN).toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
    }

    /**
     * LocalDate转毫秒级时间戳
     */
    public static Long localDateTime2Milli(LocalDateTime localDateTime) {
        return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }

    /**
     * LocalDate转秒级时间戳
     */
    public static Long localDate2Second(LocalDate localDate) {
        return LocalDateTime.of(localDate, LocalTime.MIN).toInstant(ZoneOffset.ofHours(8)).getEpochSecond();
    }

    /**
     * LocalDate转秒级时间戳
     */
    public static Long localDateTime2Second(LocalDateTime localDateTime) {
        return localDateTime.toEpochSecond(ZoneOffset.of("+8"));
    }

    public static LocalDateTime getTime(int date) {
        return LocalDateTime.now().plusDays(date);
    }

    public static LocalDateTime getTimeAddHours(int hours) {
        return LocalDateTime.now().plusHours(hours);
    }

    public static void main(String[] args) {
//        System.err.println(timeToString(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"));
        long currentTimeMillis = System.currentTimeMillis();
        LocalDateTime now = LocalDateTime.now();
//        System.err.println(currentTimeMillis);

        System.err.println(getTime(-6));
//        System.err.println(localDateTime2Milli(now));
    }
}
